<?php
/**
 * Created by PhpStorm.
 * User: James
 * Date: 2016/9/13
 * Time: 15:26
 */

namespace Weixin\Controller;
use Common\Controller\AdminbaseController;

class SettingController extends AdminbaseController
{
    protected $options_model;

    function _initialize() {
        parent::_initialize();
//        $this->options_model = D("Common/Options");
        $this->options_model = new \Common\Model\OptionsModel();
    }
    public function site(){
        $weixinOptions =$this->options_model->where(array("option_name"=>'site_weixin'))->find();

    if ($weixinOptions){
        $this->assign((array)json_decode($weixinOptions['option_value']));
        $this->assign("option_id",$weixinOptions['option_id']);
    }
        $this->display();
    }

    function site_post(){
        if (IS_POST) {
            if(isset($_POST['option_id'])){
                $data['option_id']=intval($_POST['option_id']);
            }

            $configs["WEIXIN_APPID"]=$_POST['options']['appid'];
            $configs["WEIXIN_APPSECRET"]=$_POST['options']['appsecret'];
            $configs["WEIXIN_TOKEN"]=$_POST['options']['token'];

            sp_set_dynamic_config($configs);//sae use same function

            $data['option_name']="site_weixin";
            $data['option_value']=json_encode($_POST['options']);
            if($this->options_model->where("option_name='site_weixin'")->find()){
                $r=$this->options_model->where("option_name='site_weixin'")->save($data);
            }else{
                $r=$this->options_model->add($data);
            }

            if ($r!==false) {
                $this->success("保存成功！");
            } else {
                $this->error("保存失败！");
            }

        }
    }

}